package com.sinoglobal.dumping.dumplinganim;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.activity.Dumpling_MainActivity;
import com.sinoglobal.dumping.api.Dumpling_RemoteImpl;
import com.sinoglobal.dumping.base.Dumpling_SinoAsyncTask;
import com.sinoglobal.dumping.base.Dumpling_SinoBaseActivity;
import com.sinoglobal.dumping.base.Dumpling_SinoConstans;
import com.sinoglobal.dumping.bean.Dumpling_LogOutAcquireDumpling;
import com.sinoglobal.dumping.bean.Dumpling_LogOutAcquireDumplingBean;
import com.sinoglobal.dumping.bean.Dumpling_LogOutAcquireDumplingResultList;
import com.sinoglobal.dumping.fragment.Dumpling_HomeFragment;
import com.sinoglobal.dumping.util.DumplingCache;
import com.sinoglobal.dumping.util.Dumpling_HttpMethodSet;
import com.sinoglobal.dumping.view.Dumpling_GetDumplingDialog;
import com.sinoglobal.dumping.view.Dumpling_GetDumplingDialogAdapter;
import com.sinoglobal.wallet.app.SinoValueManager;

import static com.sinoglobal.dumping.base.Dumpling_SinoConstans.DUMPLING_COUNT;
import static com.sinoglobal.dumping.base.Dumpling_SinoConstans.DUMPLING_ID_LIST;
import static com.sinoglobal.dumping.base.Dumpling_SinoConstans.DUMPLING_NO_LOGIN_COUNT;
import static com.sinoglobal.dumping.util.DumplingUtils.getCount;
import static com.sinoglobal.dumping.util.DumplingUtils.getDumplingIds;
import static com.sinoglobal.dumping.util.DumplingUtils.showDialog;

/**
 * Created by KeZhuang on 2016/1/27.
 */
public class DumplingAnimManager {
    private boolean isAnimator;
    private Dumpling_HomeFragment mFragment;
    private Dumpling_MainActivity mActivity;
    private ImageView mPotLogo;
    private TextView mTip;
    public Dumpling_GetDumplingDialog mFailedDialog;
    private FrameLayout mAnimatorLayout;
    private DumplingCache mDumplingCache;
    private ImageView mDumplingPic;
    private Dumpling_LogOutAcquireDumplingBean dumplingBean;
    private Dumpling_LogOutAcquireDumplingResultList dumplingResultList;
    private Dumpling_LogOutAcquireDumpling dumpling;
    public DumplingAnimManager(final Dumpling_HomeFragment fragment){
        this.mFragment = fragment;
        mActivity = (Dumpling_MainActivity) this.mFragment.getContext();
        mAnimatorLayout = this.mFragment.getView(R.id.lao_layout);
        mDumplingCache = new DumplingCache(fragment.getContext());
        mFailedDialog  = new Dumpling_GetDumplingDialog(mActivity);
        mPotLogo = this.mFragment.getView(R.id.guo_logo);
        mTip = this.mFragment.getView(R.id.text_1);
        mDumplingPic = mFragment.getView(R.id.jiaozi);
        mFailedDialog.initClick(
                new Dumpling_GetDumplingDialogAdapter() {
                    @Override
                    public void onFailureSureClick() {
                        mActivity.hideView(mDumplingPic);
                        super.onFailureSureClick();
                    }
                }
        );
    }
    public void getAnimation(){
        final boolean isLogin = !Dumpling_SinoBaseActivity.isNoLogIn(this.mFragment.getContext());
        int acquireCount = getCount(this.mFragment.getContext());
        if (isAnimator) {
            mActivity.hideViewForGone(mTip);
            mActivity.showToast("正在捞取中...请稍等");
            return;
        }
        if (mFragment.getMaxCount() < 0) {
            mActivity.showToast("锅内饺子不足");
            return;
        }
        if (Dumpling_HomeFragment.DEBUG) {
            acquireCount = 10;
        }
        if (acquireCount <= 0) {
            mFailedDialog.showGetDumplingFailure();
            return;
        }
        if (!isLogin) {
            acquirelogOutDumpling();
        }
        setRunning(true);
        float offset1 = mPotLogo.getBottom() - mAnimatorLayout.getMeasuredHeight();
        float offset2 = -(float) (mPotLogo.getWidth() / 4 * 2);
        float offset3 = mPotLogo.getMeasuredWidth() / 12;
        mAnimatorLayout.animate().rotation(-30).setDuration(500).start();
        mAnimatorLayout.animate().translationY(offset1).translationX(offset3).setDuration(500).start();
        ObjectAnimator animator = ObjectAnimator.ofFloat(mAnimatorLayout, "translationX", offset3, offset2, offset3);
        animator.setDuration(900);
        animator.setStartDelay(400);
        animator.start();
        animator.setRepeatCount(2);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mActivity.showView(mDumplingPic);
            }
        });
        PropertyValuesHolder holder0 = PropertyValuesHolder.ofFloat("translationX", 2f);
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("translationY", offset3);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("rotation", -30f, 0f);
        ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(mAnimatorLayout, holder2, holder1,holder0);
        oa.setDuration(900).setStartDelay(3000);
        oa.start();
        oa.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                setRunning(false);
                if (dumplingBean != null) {
                    if (dumplingBean.getCode() == 100) {
                        //捞取饺子次数控制
                        int acquireCount = getCount(mActivity);
                        if (isLogin) {
                            acquireCount = Integer.parseInt(dumplingResultList.getUserHasNum());
                        }
                        if (acquireCount > 0) {
                            acquireCount--;
                            mDumplingCache.put(DUMPLING_COUNT,acquireCount);
                        }
                        mActivity.showView(mTip);
                        //存取饺子id
                        String dumplingId = getDumplingIds(mActivity);
                        dumplingId += dumpling.getPrizeId() + ",";
                        mDumplingCache.put(DUMPLING_ID_LIST,dumplingId);
                        mFragment.getHelper().equalsDumplingType();
                        mFragment.getHelper().setUpTip();
                        if(mFragment.isShowActviity()){
                            showDialog(mActivity, dumplingBean);
                        }
                    } else if (dumplingBean.getCode() == 102) {
                        mFailedDialog.showGetDumplingFailure();
                    }
                } else {
                    mActivity.showToast("网络不佳,请重试");
                }
            }
        });
    }

    public boolean isRunning(){
        return isAnimator;
    }
    public void setRunning(boolean running){
        isAnimator = running;
    }
    public void acquirelogOutDumpling() {
        new Dumpling_SinoAsyncTask<Object, Void, Dumpling_LogOutAcquireDumplingBean>(mActivity, false) {

            @Override
            public Dumpling_LogOutAcquireDumplingBean before(Object... params) throws Exception {
                return Dumpling_RemoteImpl.getInstance().logoutStart();
            }

            @Override
            public void after(Dumpling_LogOutAcquireDumplingBean dumplingLogOutAcquireDumplingBean) {
                if(dumplingLogOutAcquireDumplingBean!=null){
                    mFragment.setAcquireDumplingBean(dumplingLogOutAcquireDumplingBean);
                    dumplingBean = dumplingLogOutAcquireDumplingBean;
                    if (dumplingLogOutAcquireDumplingBean.getCode() == 100) {
                        dumplingResultList = dumplingBean.getResultList();
                        dumpling = dumplingResultList.getDumpling();
                        if (Dumpling_SinoBaseActivity.isNoLogIn(mActivity)) {
                            int mNoLogInNum = (Integer) mDumplingCache.get(DUMPLING_NO_LOGIN_COUNT,0);
                            mNoLogInNum++;
                            mDumplingCache.put(DUMPLING_NO_LOGIN_COUNT,mNoLogInNum);
                        }
                    }
                }
            }

            @Override
            public void exception() {

            }
        }.execute();
    }
    public void acquireloginUserDumpling() {
        new Dumpling_SinoAsyncTask<Object, Void, Dumpling_LogOutAcquireDumplingBean>(mActivity, false) {

            @Override
            public Dumpling_LogOutAcquireDumplingBean before(Object... params) throws Exception {
                return Dumpling_RemoteImpl.getInstance().loginUserDumpling();
            }

            @Override
            public void after(Dumpling_LogOutAcquireDumplingBean acquireDumplingBean) {
                if(acquireDumplingBean!=null){
                    mFragment.setAcquireDumplingBean(acquireDumplingBean);
                    if (acquireDumplingBean.getCode() == Dumpling_SinoConstans.SUCCESS_CODE) {
                        dumplingBean = acquireDumplingBean;
                        dumplingResultList = dumplingBean.getResultList();
                        dumpling = dumplingResultList.getDumpling();
                        mDumplingCache.put(DUMPLING_COUNT,Integer.parseInt(dumplingResultList.getUserHasNum()));
                        getAnimation();
                        Dumpling_HttpMethodSet.isGetSucceed = true;
                    } else if (acquireDumplingBean.getCode() == 102) {
                        dumplingBean = acquireDumplingBean;
                        mDumplingCache.put(DUMPLING_COUNT,0);
                        mFailedDialog.showGetDumplingFailure();
                    } else {
                        dumplingBean = acquireDumplingBean;
                        mDumplingCache.put(DUMPLING_COUNT,0);
                        mFailedDialog.showGetDumplingFailure();
                    }
                }
            }

            @Override
            public void exception() {

            }
        }.execute();
    }
}
